import java.util.Comparator;
import java.util.PriorityQueue;


class IntCmp implements Comparator<Integer>{//大跟堆的做法  //小跟堆则o1.compareTo(o2)  //源码默认为小跟堆
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);
    }
}


public class Test2 {

    //最小的K个数
    //https://leetcode.cn/problems/smallest-k-lcci/
    //第K小的数
    public int[] smallestK(int[] arr,int k){
        int[] ret = new int[k];
        if (arr == null || k == 0) return ret;

        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(k,new IntCmp());
        for (int i = 0; i < k; i++) {
            priorityQueue.offer(arr[i]);
        }

        for (int i = k; i <arr.length;  i++) {
            int peekVal = priorityQueue.peek();
            if (arr[i] < peekVal){
                priorityQueue.poll();
                priorityQueue.offer(arr[i]);
            }
        }

        for (int i = 0; i < k; i++) {
            ret[i] = priorityQueue.poll();
        }

        return ret;
    }

    public int[] smallestK1(int[] arr,int k){
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();

        for (int i = 0; i < arr.length; i++) {
            priorityQueue.offer(arr[i]);
        }

        int[] ret = new int[k];
        for (int i = 0; i < k; i++) {
            ret[i] = priorityQueue.poll();
        }

        return ret;
    }

    public static void main(String[] args) {
        PriorityQueue<Integer> priorityQueue =new PriorityQueue<>(new IntCmp());
        priorityQueue.offer(11);
        priorityQueue.offer(12);
        System.out.println(priorityQueue.peek());
    }
}
